package com.ldl.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.bean.BeanUtil;

import com.ldl.dao.impl.LdlDaoImpl;
import com.ldl.dto.Search;
import com.ldl.entity.Ldl;
import com.ldl.service.LdlService;
import com.ldl.vo.PageVO;


/**
 * @author Administrator
 * Author : L.D.L
 * E-mail : 1419485293@qq.com
 * Date : 2020年7月24日下午8:27:06
 */
public class LdlServiceImpl implements LdlService{
	
	private static Logger log1 = LoggerFactory.getLogger(LdlServiceImpl.class) ;

	private LdlDaoImpl ldlDaoImpl = new LdlDaoImpl();

	@Override
	public List<Ldl> list(int current,int len) {
		List<Ldl> list = new ArrayList<Ldl>();
		try {
			List<Ldl> list1 = ldlDaoImpl.selectAll((current-1)*len,len);
			list1.forEach((entity)->{
				Ldl u = new Ldl();
				//vo.setArticleId(entity.getArticleId());
				//...
				BeanUtil.copyProperties(entity, u);
				list.add(u);
			});
			
		} catch (SQLException e) {
//			e.printStackTrace();//以后禁止使用错误打印和System.out。日志
			log1.debug("{}",e);
		}
		return list;
	}

	@Override
	public int add(Ldl ldl) {
		try{
			return ldlDaoImpl.add(ldl);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new RuntimeException("保存用户数据失败", ex);
		}
	}


	@Override
	public int delete(int ldlId) {
		try{
			return ldlDaoImpl.deleteById(ldlId);
		}catch(Exception ex){
			throw new RuntimeException("删除用户失败", ex);
		}
	}

	
	@Override
	public Ldl selete(int ldlId) {
		try {
			return ldlDaoImpl.selectById(ldlId);
		} catch (SQLException e) {
			e.printStackTrace();//日志
			throw new RuntimeException("查询用户数据失败", e);
		}
	}

	@Override
	public int update(Ldl ldl) {
		try {
			return ldlDaoImpl.updateById(ldl);
		} catch (SQLException e) {
			e.printStackTrace();//日志
			throw new RuntimeException("修改用户数据失败", e);
		}
	}

	@Override
	public int count(String name) {
		try {
			return ldlDaoImpl.countAll(name);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询用户数据条数失败", e);
		}
	}
	
	@Override
	public PageVO<Ldl> page(int current, int len) {
		try {
			PageVO<Ldl> page = new PageVO<Ldl>(ldlDaoImpl.countAll(""), current, len);
			return page.setList(ldlDaoImpl.selectAll(page.getOffset(), page.getLen()));
		} catch (SQLException e) {
			e.printStackTrace();// 日志
			throw new RuntimeException("获取用户分页数据失败", e);
		}
	}

	@Override
	public List<Ldl> search(String key ,int start,int end) {
		List<Ldl> list = null;
		try {
			list = ldlDaoImpl.search(key,(start-1)*end,end);
		} catch (SQLException e) {
//			e.printStackTrace();
			log1.debug("{}",e);
		}
		return list;
	}
	
}
